﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace Study_Time
{
    public partial class StudyButton : PictureBox
    {
        private Boolean open = false;
        public String UserProvidedAnswer { get; set; }
        private String userProvidedGuess = "";

        public StudyButton(int x, int y, String text)
        {
            //Set the image to a gray button.
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream imageStream = assembly.GetManifestResourceStream("Study_Time.Resources.GrayButton.png");
            this.Image = Image.FromStream(imageStream);
            this.BackColor = Color.Transparent;

            open = false;
            UserProvidedAnswer = text;

            //Set the size of the picture box.
            this.Width = 10;
            this.Height = 10;

            //Set the location of the picture box.
            Point location = new Point(x, y);
            this.Location = location;

            // Add mouse click event handler
            this.MouseClick += new MouseEventHandler(SpotButton_MouseClick);
        }

        private void SpotButton_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!open)
                    Open();
                else
                    Close();
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (Program.Mode == Program.StudyTimeMode.Edit)
                {
                    DialogResult result = MessageBox.Show("Delete the button with text: \"" + UserProvidedAnswer + "\"?", "Delete Button?", MessageBoxButtons.YesNo);

                    if (result == DialogResult.Yes)
                        Dispose();
                }
            }
        }

        private Boolean IsGuessCorrect(String guess)
        {
            if (guess.ToUpper().Equals(UserProvidedAnswer.ToUpper()))
                return true;
            else
                return false;
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Close();
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }

        public void Reset()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream imageStream = assembly.GetManifestResourceStream("Study_Time.Resources.GrayButton.png");
            this.Image = Image.FromStream(imageStream);

            userProvidedGuess = "";
        }

        public void Open()
        {
            // Change clicked value
            open = true;

            //Change the size of the PictureBox in order to fit a TextBox inside.
            this.Width = 250;
            this.Height = 20;

            //Create the TextBox.
            TextBox textBox = new TextBox();
            textBox.Width = 240;
            textBox.Location = new Point(10, 0);
            textBox.KeyDown += new KeyEventHandler(TextBox_KeyDown);

            // Study mode
            if (Program.Mode == Program.StudyTimeMode.Study)
                textBox.Text = userProvidedGuess;
            // Edit mode
            else if (Program.Mode == Program.StudyTimeMode.Edit)
                textBox.Text = UserProvidedAnswer;

            //Add the TextBox to the PictureBox.
            this.Controls.Add(textBox);
            textBox.Focus();
        }

        public void Close()
        {
            // Change clicked value
            open = false;

            // Study mode
            if (Program.Mode == Program.StudyTimeMode.Study)
            {
                // Get the text box text
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i] is TextBox)
                    {
                        userProvidedGuess = Controls[i].Text;
                        break;
                    }
                }

                // Check to see if provided guess is correct.
                Boolean guessIsCorrect = IsGuessCorrect(userProvidedGuess);

                if (guessIsCorrect)
                {
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    Stream imageStream = assembly.GetManifestResourceStream("Study_Time.Resources.GreenButton.png");
                    this.Image = Image.FromStream(imageStream);
                }
                else
                {
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    Stream imageStream = assembly.GetManifestResourceStream("Study_Time.Resources.RedButton.png");
                    this.Image = Image.FromStream(imageStream);
                }
            }
            // Edit mode
            else if (Program.Mode == Program.StudyTimeMode.Edit)
            {
                // Save the text
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i] is TextBox)
                    {
                        UserProvidedAnswer = Controls[i].Text;
                        break;
                    }
                }
            }

            //Change the size back to the original 10x10.
            this.Width = 10;
            this.Height = 10;

            //Remove the TextBox.
            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i] is TextBox)
                {
                    this.Controls.RemoveAt(i);
                    return;
                }
            }
        }
    }
}
